package com.iqiao.platform.controller.admin;

import com.github.pagehelper.PageInfo;
import com.iqiao.common.exception.ParamsException;
import com.iqiao.common.exception.ResetException;
import com.iqiao.common.util.EnumUtil;
import com.iqiao.common.web.Servlets;
import com.iqiao.common.web.result.JsonResult;
import com.iqiao.common.web.result.PageResult;
import com.iqiao.common.web.result.ReturnCode;
import com.iqiao.platform.enums.MenuType;
import com.iqiao.platform.model.Menu;
import com.iqiao.platform.service.MenuService;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "menus")
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     *  查询菜单
     * @param request
     * @param response
     * @return
     */
    @GetMapping()
    public JsonResult findList(
            HttpServletRequest request,
            HttpServletResponse response) {
        Map<String, Object> params = Servlets.getParams(request);
        PageInfo<Menu> pageInfo = menuService.findPage(params);
        return PageResult.success(pageInfo);
    }

    /**
     * 保存菜单
     * @param request
     * @param response
     * @param map
     * @return
     */
    @PostMapping()
    public JsonResult save(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestBody Map<String, Object> map) {

        Long parentId = MapUtils.getLong(map, "parentId", 0L);
        String name = MapUtils.getString(map, "name");
        Integer sort = MapUtils.getInteger(map, "sort");
        String code = MapUtils.getString(map, "code");
        Integer type = MapUtils.getInteger(map, "type");
        String path = MapUtils.getString(map, "path");
        String icon = MapUtils.getString(map, "icon");

        if (StringUtils.isBlank(name)) {
            throw new ParamsException(ReturnCode.MENU_NAME_NOT_NULL, "菜单名称不能为空！");
        }
        if (StringUtils.isBlank(code)) {
            throw new ParamsException(ReturnCode.MENU_CODE_NOT_NULL, "菜单编号不能为空！");
        }
        if (sort == null) {
            throw new ParamsException(ReturnCode.MENU_SORT_NOT_NULL, "菜单排序不能为空！");
        }
        if (!EnumUtil.isValidEnum(MenuType.class, type)) {
            throw new ParamsException(ReturnCode.MENU_TYPE_NOT_EXIST, "菜单类型不存在！");
        }
        if (type.equals(MenuType.menu.getValue()) && StringUtils.isBlank(path)) {
            throw new ParamsException(ReturnCode.MENU_PATH_NOT_NULL, "菜单路径不能为空！");
        }
        Menu menu = new Menu();
        menu.setParentId(parentId);
        menu.setName(StringUtils.trim(name));
        menu.setCode(StringUtils.trim(code));
        menu.setSort(sort);
        menu.setType(EnumUtil.valueOfEnum(MenuType.class, type));
        menu.setPath(path);
        menu.setIcon(icon);
        menuService.save(menu);
        return JsonResult.success(menu, "菜单信息保存成功！");
    }

    /**
     * 修改菜单
     * @param request
     * @param response
     * @param id 菜单id
     * @param map
     * @return
     */
    @PutMapping("{id}")
    public JsonResult save(
            HttpServletRequest request,
            HttpServletResponse response,
            @PathVariable(value = "id") Long id,
            @RequestBody Map<String, Object> map) {

        Long parentId = MapUtils.getLong(map, "parentId", 0L);
        String name = MapUtils.getString(map, "name");
        String code = MapUtils.getString(map, "code");
        Integer sort = MapUtils.getInteger(map, "sort");
        Integer type = MapUtils.getInteger(map, "type");
        String path = MapUtils.getString(map, "path");
        String icon = MapUtils.getString(map, "icon");

        if (StringUtils.isBlank(name)) {
            throw new ParamsException(ReturnCode.MENU_NAME_NOT_NULL, "菜单名称不能为空！");
        }
        if (StringUtils.isBlank(code)) {
            throw new ParamsException(ReturnCode.MENU_CODE_NOT_NULL, "菜单编号不能为空！");
        }
        if (sort == null) {
            throw new ParamsException(ReturnCode.MENU_SORT_NOT_NULL, "菜单排序不能为空！");
        }
        if (!EnumUtil.isValidEnum(MenuType.class, type)) {
            throw new ParamsException(ReturnCode.MENU_TYPE_NOT_EXIST, "菜单类型不存在！");
        }
        if (type.equals(MenuType.menu.getValue()) && StringUtils.isBlank(path)) {
            throw new ParamsException(ReturnCode.MENU_PATH_NOT_NULL, "菜单路径不能为空！");
        }
        Menu menu = new Menu();
        menu.setId(id);
        menu.setParentId(parentId);
        menu.setName(StringUtils.trim(name));
        menu.setCode(StringUtils.trim(code));
        menu.setSort(sort);
        menu.setType(EnumUtil.valueOfEnum(MenuType.class, type));
        menu.setPath(path);
        menu.setIcon(icon);
        menuService.modify(menu);
        return JsonResult.success(menu, "菜单修改成功！");
    }

    /**
     * 删除菜单
     * @param request
     * @param response
     * @param id 菜单id
     * @param map
     * @return
     */
    @DeleteMapping("{id}")
    public JsonResult delete(
            HttpServletRequest request,
            HttpServletResponse response,
            @PathVariable(value = "id") Long id,
            @RequestBody Map<String, Object> map) {

        menuService.delete(id);
        return JsonResult.success(null, "菜单删除成功！");
    }
}
